package com.kkwrite.regimen.provider.controller.user;

import javax.annotation.Resource;

import com.kkwrite.regimen.common.constant.Constant;
import com.kkwrite.regimen.common.entity.dto.login.InviterInfoDTO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kkwrite.regimen.common.entity.dto.BaseDTO;
import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.user.PageableUserDTO;
import com.kkwrite.regimen.common.entity.dto.user.RegUserDTO;
import com.kkwrite.regimen.common.entity.dto.user.RegUserInDTO;
import com.kkwrite.regimen.common.entity.dto.user.RegUserOutDTO;
import com.kkwrite.regimen.common.entity.dto.user.UserOutDTO;
import com.kkwrite.regimen.provider.exception.ServiceException;
import com.kkwrite.regimen.provider.service.user.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

/**
 * Created by lisha on 2018/7/11 21:18.
 *
 * @author lisha
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Resource(name = "regUserService")
    private UserService userService;

    @PostMapping(value = "/bindinviter/{userId}")
    public OutDTO saveInviter(@RequestBody InviterInfoDTO infoDTO, @PathVariable Integer userId) {
        try {
            userService.saveInviter(infoDTO, userId);
        } catch (Exception e) {
            return OutDTO.of(Constant.BOOLEAN_STRING_FALSE, e.getMessage());
        }
        return OutDTO.of(Constant.BOOLEAN_STRING_TRUE, "OK", "OK");
    }

    @PostMapping(value = "/checkOrSave")
    @HystrixCommand(fallbackMethod = "checkOrSaveError")
    public BaseDTO saveUser(@RequestBody RegUserDTO userDTO) {
        return userService.saveUser(userDTO);
    }

    public BaseDTO checkOrSaveError(RegUserDTO userDTO) {
        return errorBaseResult();
    }

    private BaseDTO errorBaseResult() {
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setCode("-1");
        baseDTO.setMessage("服务异常！");
        return baseDTO;
    }

    @PostMapping(value = "/update")
    @HystrixCommand(fallbackMethod = "updateUserError")
    public RegUserOutDTO updateUser(@RequestBody RegUserInDTO inDTO) {
        return userService.updateUser(inDTO);
    }
    
    @GetMapping(value = "/queryUserByInviterId")
	@HystrixCommand(fallbackMethod = "queryUserByInviterIdError")
    public OutDTO<PageableUserDTO> queryUserByInviterId(Integer page, Integer size,Long inviterId, Integer sortType) {
    	OutDTO<PageableUserDTO> outDTO = new OutDTO<>();
		try {
			PageableUserDTO pageableUserDTO = userService.queryUserByInviterId(page, size, inviterId, sortType);
			outDTO.setSuccess("true");
			outDTO.setData(pageableUserDTO);
			return outDTO;
		} catch (ServiceException e) {
			return new OutDTO<>("failed", e.getMessage());
		}
    }

    public RegUserOutDTO updateUserError(RegUserInDTO inDTO) {
        return new RegUserOutDTO("false", "服务异常！");
    }

	public OutDTO<PageableUserDTO> queryUserByInviterIdError(Integer page,
			Integer size, Long inviterId, Integer sortType) {
		return new OutDTO("false", "服务异常！");
	}

    @GetMapping(value = "/getInfo/{userId}")
    public OutDTO<UserOutDTO> queryUserInfo(@PathVariable String userId) {
        return userService.queryUser(userId);
    }

}
